< previous page page_133 next page >

Page 133
CenterText begins by creating the rotated font and installing it in the picture box as described earlier in this chapter. It then uses the GetTextMetrics API function to get information about the font's dimensions. It uses the results to determine the height of the text with and without its internal leading space. It uses TextWidth to determine the string's width.
CenterText then uses the geometry shown in Figure 4.10 to find the dimensions of the bounding boxes that surround the rotated text. The values total_bound_wid and total_bound_hgt give the dimensions of the box including the internal leading space. The values text_bound_wid and text_bound_hgt give the dimensions of the bounding box that does not include the internal leading space.
The routine then calculates the starting point for the text in small steps. It begins with the point (x1, y1) at the desired center position. It subtracts half of the width and height of the text bounding box. That moves (x1, y1) to the upper-left corner of the text bounding box.
The program then adds W * Sin(q) to y1. That moves the point (x1, y1) to the upper-left corner of the text. In Figure 4.10, this is right next to the upper-left corner of the letter M.
Next CenterText subtracts the differences in the widths and heights of the two bounding boxes from the coordinates. That moves the point (x1, y1) to the upper-left corner of the outer bounding box. This is the position where the program must start printing to properly center the string.
The following code fragment shows the most important parts of subroutine CenterText. The routine is rather long, so only its parts that deal with positioning the rotated text are shown here. You can find the entire routine in the CentText program on the CD-ROM.
e10a6a0a59f035e870d90cca05b85fce.gif
    :
' Get the font metrics.
GetTextMetrics pic.hdc, text_metrics
internal_leading = _
    pic.ScaleY(text_metrics.tmInternalLeading, _
    vbPixels, pic.ScaleMode)
total_hgt = _
    pic.ScaleY(text_metrics.tmHeight, _
    vbPixels, pic.ScaleMode)
text_hgt = total_hgt - internal_leading
text_wid = pic.TextWidth(txt)

' Get the bounding box geometry.
theta = nEscapement / 10 / 180 * PI
phi = PI / 2 - theta
text_bound_wid = text_hgt * Cos(phi) + _
    text_wid * Cos(theta)
text_bound_hgt = text_hgt * Sin(phi) + _
    text_wid * Sin(theta)
total_bound_wid = total_hgt * Cos(phi) + _
    text_wid * Cos(theta)
total_bound_hgt = total_hgt * Sin(phi) + _
    text_wid * Sin(theta)

 
< previous page page_133 next page >